package com.lk.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lk.springboot.service.GameService;
import com.lk.springboot.service.LoginService;
import com.lk.springboot.utils.RandomUtil;
import com.lk.springboot.vo.Result;
import com.lk.springboot.vo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lk
 * @date 2020/1/10 0010 23:02
 * @description: 游戏控制模块
 */
@RestController
@RequestMapping("/game")
public class GameContoller {

    @Autowired
    GameService gameService;
    @Autowired
    LoginService loginService;

    /**
     * 获取游戏中的碎片
     *
     * @param token
     * @param sp_id
     * @return
     */
    @RequestMapping(value = "/getphoto", method = RequestMethod.POST)
    public String getGameSp(@CookieValue("token") String token,
                            @RequestParam(value = "sp_id") String sp_id) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            gameService.addPhoto(u_id, sp_id);
            Result result = new Result(ResultCode.SUCCESS);
            result.setData("已获得该碎片");
            return JSON.toJSONString(result);
        }

    }


    /**
     * 游戏前 随机 分发拼图碎片
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/givephoto", method = RequestMethod.GET)
    public String giveSP(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = gameService.giveSP(u_id, 0);
            return result;
        }
    }

    /**
     * 根据游戏成绩获取相应的积分
     *
     * @param token
     * @param gameType
     * @param gameScore
     * @return
     */
    @RequestMapping(value = "/getscore", method = RequestMethod.POST)
    public String getScore(@CookieValue("token") String token,
                           @RequestParam(value = "gameType") String gameType,
                           @RequestParam(value = "gameScore") int gameScore) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            int scoreNum = gameService.addScore(u_id, gameType, gameScore);
            Result result = new Result(ResultCode.SUCCESS);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("score", scoreNum);
            if (gameType.equals("offline")) {
                jsonObject.put("recordBreaking", gameService.getIsRecordBreaking(u_id, gameScore));//是否破纪录
            }
            result.setData(jsonObject);
            return JSON.toJSONString(result);
        }
    }

    /**
     * 获取线下游戏最高分
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/getmaxgamescore", method = RequestMethod.GET)
    public String getMaxGameScore(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = gameService.getMaxGameScore(u_id);
            return result;
        }

    }

    /**
     * 将用户设为玩游戏的新用户
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/setnewgameuser", method = RequestMethod.GET)
    public String getGameSp(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            gameService.setNewGameUser(u_id);
            Result result = new Result(ResultCode.SUCCESS);
            result.setData("success");
            return JSON.toJSONString(result);
        }

    }

    /**
     * AR发放拼图奖励
     */
    @RequestMapping(value = "/vr", method = RequestMethod.GET)
    public String giveVRphotos(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            Result result = gameService.getSP(u_id);//拼图奖励
            int type = (new RandomUtil()).randomGetAwardType(10, 1);
            if (type > 5) {//物质奖励
                if (!"Nothing".equals(result.getData())) {
                    result = gameService.getTicket(u_id);
                }
            }
            return JSON.toJSONString(result);
        }
    }

    /**
     * 为VR准备奖励
     */
    @RequestMapping(value = "/vr", method = RequestMethod.POST)
    public String getVRphotos(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            gameService.prepareSP(u_id);
            Result result = new Result(ResultCode.SUCCESS);
            result.setData("正在准备奖励");
            return JSON.toJSONString(result);
        }
    }


    /**
     * 发留言
     */
    @RequestMapping(value = "/msg", method = RequestMethod.POST)
    public String writeMsg(@CookieValue("token") String token,
                           @RequestParam(value = "msg") String msg) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            Result result = gameService.writeMsg(u_id, msg);
            return JSON.toJSONString(result);
        }
    }

    /**
     * 获取留言
     */
    @RequestMapping(value = "/msg", method = RequestMethod.GET)
    public String readMsg(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData("");
            return JSON.toJSONString(ru);
        } else {
            Result result = new Result(ResultCode.SUCCESS);
            List<String> list = gameService.readMsg(u_id);
            result.setData(list);
            return JSON.toJSONString(result);
        }
    }
}
